package View;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;

import Controller.AddressBookController;
import Controller.DetailsContactListener;

/**
 * View to display the details of a contact
 * 
 */	

public class DetailsContactView extends JDialog {
	
	public static final String FIRSTNAME = "First name: ";
	public static final String LASTNAME= "Last Name: ";
	public static final String GROUP = "Group: " ;
	public static final String HOMEADDRESS = "Home Address: ";
	public static final String COMPANYNAME = "Company Name: ";
	public static final String COMPANYADDRESS = "Company Address: ";
	public static final String PERSPHONE = "Personal Phone: ";
	public static final String PROPHONE = "Professional Phone: ";
	public static final String WEBSITE = "Web Site: ";
	public static final String EMAIL = "Email: ";
	public static final String BIRTHDATE = "Birthdate: ";
	public static final String IMAGE = "Image: ";
	public static final String DATE ="Date: ";
	public static final String OK = "OK";
	public static final String CANCEL = "Cancel";
	
	public static final String RESOURCES_FOLDER = "resources/";
	
	public static final String HTML_BEGIN = "<html>";
	public static final String HTML_END ="</html>";
	public static final String LINK_BEGIN ="<a href=\"";
	public static final String LINK_END ="\">";
	public static final String LINK_BALISE_END ="</a>";
	public static final String NEXT_LIGNE = "<br>";
	
	public static final String SPACE_ENCODED = "%20";
	public static final String SPACE_CHAR = " ";
	public static final String FILE_BEGINING = "file:/" ;
	
	private DetailsContactView that = this;
	
	private StringBuilder strBuilder,strBuilder1,strBuilder2,strBuilderImage;

	private AddressBookController controller;
	JLabel lDetails,lEmail,lWebSite,lImage;
	JButton bOK;
	String details,url,address;
	private String firstName,lastName,group, homeAddress,companyName,companyAddress,persPhone,proPhone,webSite,email,birthDate,imgURL;
	private String labelEmail,labelWebSite;
	private Date date;
	JPanel p1,p2,p3;
	private GridBagConstraints constraints1,constraints2,constraints3,constraints4 ;
	private GridBagLayout gridBagLayout;
	private ImageIcon image;
	
	//Default constructor
	
	public DetailsContactView (AddressBookController controller) {
		this.controller = controller;
		
		//load the details into the variables
		firstName = controller.getContact().getFirstName();
		lastName= controller.getContact().getLastName();
		group= controller.getContact().getGroup();
		homeAddress= controller.getContact().getHomeAddress();
		companyName= controller.getContact().getCompanyName();
		companyAddress= controller.getContact().getCompanyAddress();
		persPhone= controller.getContact().getPersPhone();
		proPhone= controller.getContact().getProPhone();
		webSite= controller.getContact().getWebSite();
		email= controller.getContact().getEmail();
		birthDate= controller.getContact().getBirthDate();
		date = controller.getContact().getDate();
		imgURL = controller.getContact().getImgURL();
		
		
		
		//Text on the Jlabel detail
		strBuilder = new StringBuilder();		
		
		strBuilder.append(HTML_BEGIN);
		
			strBuilder.append(FIRSTNAME).append(firstName).append(NEXT_LIGNE).append(LASTNAME).append(lastName).append(NEXT_LIGNE);
			strBuilder.append(GROUP).append(group).append(NEXT_LIGNE).append(HOMEADDRESS).append(homeAddress).append(NEXT_LIGNE);
			strBuilder.append(COMPANYNAME).append(companyName).append(NEXT_LIGNE).append(COMPANYADDRESS).append(companyAddress).append(NEXT_LIGNE);
			strBuilder.append(PERSPHONE).append(persPhone).append(NEXT_LIGNE).append(PROPHONE).append(proPhone).append(NEXT_LIGNE);
			strBuilder.append(WEBSITE).append(webSite).append(NEXT_LIGNE).append(EMAIL).append(email).append(NEXT_LIGNE);
			strBuilder.append(BIRTHDATE).append(birthDate).append(NEXT_LIGNE).append(DATE).append(date);
			
		strBuilder.append(HTML_END);
		
		details=strBuilder.toString();
		
		//Text on the JLabel WebSite
		strBuilder1 = new StringBuilder();
		
		strBuilder1.append(HTML_BEGIN);
			strBuilder1.append(WEBSITE).append(LINK_BEGIN).append(webSite).append(LINK_END).append(webSite).append(LINK_BALISE_END);		
		strBuilder1.append(HTML_END);	
		
		labelWebSite = strBuilder1.toString();
		
		
		//Text on the JLabel Email
		strBuilder2 = new StringBuilder();
		
		strBuilder2.append(HTML_BEGIN);
			strBuilder2.append(EMAIL).append(LINK_BEGIN).append(email).append(LINK_END).append(email).append(LINK_BALISE_END);		
		strBuilder2.append(HTML_END);
		
		labelEmail = strBuilder2.toString();
		
		//Creation of the JLabels
		lDetails = new JLabel(details);
		lEmail = new JLabel(labelEmail);
		lWebSite = new JLabel(labelWebSite);
		
		
		
		//p1 holds the details and the contact picture
		p1= new JPanel(gridBagLayout = new GridBagLayout());

		constraints1 = new GridBagConstraints();
		constraints1.gridx=1;
		constraints1.gridy=1;
		gridBagLayout.setConstraints(lDetails,constraints1);
		
		constraints2 = new GridBagConstraints();
		constraints2.gridx=1;
		constraints2.gridy=2;
		constraints2.anchor=GridBagConstraints.WEST;
		gridBagLayout.setConstraints(lEmail,constraints2);
		
		constraints3 = new GridBagConstraints();
		constraints3.gridx=1;
		constraints3.gridy=3;
		constraints3.anchor=GridBagConstraints.WEST;
		gridBagLayout.setConstraints(lWebSite,constraints3);
		
		
		p1.add(lDetails);
		p1.add(lEmail);
		p1.add(lWebSite);
		
		lEmail.setCursor(new Cursor(Cursor.HAND_CURSOR));
		lEmail.addMouseListener(new MailOpenAdapter());
		
		lWebSite.setCursor(new Cursor(Cursor.HAND_CURSOR));
		lWebSite.addMouseListener(new URLOpenAdapter());		
		
		
		//p2 holds the buttons
		p2 = new JPanel(new FlowLayout());
		p2.add(bOK= new JButton(OK));
		
		//p3 holds the contact's image
		strBuilderImage = new StringBuilder();
		
		
		strBuilderImage.append(this.getClass().getResource(RESOURCES_FOLDER)).append(imgURL);
		String imgPath = strBuilderImage.toString();
		String imgPathSplit []=imgPath.split(FILE_BEGINING);
		imgPath = imgPathSplit[1];
		imgPath=imgPath.replace(SPACE_ENCODED, SPACE_CHAR);
		
		image = new ImageIcon(imgPath);
		
		//Resize the image
		Image img = image.getImage(); 
		BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); 
		Graphics g = bi.createGraphics(); 
		g.drawImage(img, 0, 0, 150, 180*img.getHeight(null)/img.getWidth(null), null); //resize and keep the same ratio
		final ImageIcon resizedImage = new ImageIcon(bi);
		
		
		
		p3 = new JPanel(new FlowLayout()){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			protected void paintComponent(Graphics g)
			{
				//  Display image
				g.drawImage(resizedImage.getImage(), 0, 0, null); 	
				super.paintComponent(g);
			}
		};
		p3.setOpaque(false);
		
		p3.setPreferredSize(new Dimension(150,180));
		
		
		
		//Add panels to the frame
		getContentPane().add(p1,BorderLayout.CENTER);
		getContentPane().add(p2,BorderLayout.SOUTH);
		getContentPane().add(p3,BorderLayout.WEST);
		
		//Add action listners
		DetailsContactListener listner= new DetailsContactListener(that);
		bOK.addActionListener(listner);
	}
	
	
	public AddressBookController getController () {
		return controller;
	}
	

	
	
	//Class used to open an url with the brother
	public class URLOpenAdapter extends MouseAdapter {

	 @Override
	  public void mouseClicked(MouseEvent e) {
	            if (Desktop.isDesktopSupported()) {
	                try {
	                    Desktop.getDesktop().browse(new URI(webSite));
	                } catch (Throwable t) {
	                    //
	                }
	            }
	        }
	    }
	 
	 public class MailOpenAdapter extends MouseAdapter {
		 @Override
		  public void mouseClicked(MouseEvent e) {
		            if (Desktop.isDesktopSupported()) {
		                try {
		                    Desktop.getDesktop().mail(new URI("mailto:" + email + "?subject=Hello"));
		                } catch (Throwable t) {
		                    //
		                }
		            }
		        }
		}

}



